package com.dckj.auto.common.utils;


import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.*;

/**
 * @since 1.0
 * ${tags}
 * <br><b>作者： @author WangDong</b>
 * <br>创建时间：2018/2/22 0022 9:27
 */
public class StringUtil {

    public static  String getUserId(HttpServletRequest request){
        return (String)request.getSession().getAttribute(PublicCode.USER_ID);
    }

    /**
     * 生成随机数当作getItemID
     * n ： 需要的长度
     * @return
     */
    //生成随机数字和字母,
    public static String getCode(int length) {
        StringBuffer val = new StringBuffer();
        Random random = new Random();
        //length为几位密码
        for(int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if( "char".equalsIgnoreCase(charOrNum) ) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val.append((char)(random.nextInt(26) + temp));
            } else if( "num".equalsIgnoreCase(charOrNum) ) {
                val.append(String.valueOf(random.nextInt(10)));
            }
        }
        return val.toString().toUpperCase();
    }

    /**
     * 生产ItemName随机函数
     * @param length
     * @return
     */
    private static String getItemName( int length ){
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for ( int i = 0; i < length; i++ )
        {
            int number = random.nextInt( base.length() );
            sb.append( base.charAt( number ) );
        }
        return sb.toString();
    }

    public static String  getUUId(){
        String uuid = UUID.randomUUID().toString();
        return uuid.replaceAll("-","");
    }

    public static Map<String,Object> getFileName(String fileName,String filePath,MultipartFile file) throws IOException {
        String random = getRandom(6);
        String name = file.getOriginalFilename();
        //InputStream inputStream = file.getInputStream();
        //扩展名
        String suffix = name.substring(name.lastIndexOf("."));
        LinkedHashMap<String,Object> map = new LinkedHashMap<>();
       // map.put(PublicCode.INPUT_STREAM,inputStream);
        map.put(fileName,name);
        map.put(filePath,DateUtils.getTimeCode()+random+suffix);
        return map;
    }

    public static String  getRandom(int length){
        Random random = new Random();
        StringBuffer str = new StringBuffer();
        for (int i=0;i<length;i++)
        {
            str.append(random.nextInt(10));
        }
        return str.toString();
    }

    public static String oldUrl(String oldUrl){
        if(StringUtils.isEmpty(oldUrl)){
            return "";
        }
        if(oldUrl.indexOf("/")<=0){
            return "";
        }
        return oldUrl.substring(oldUrl.lastIndexOf("/")+1,oldUrl.length());
    }

    public static String orderNo(int length){
        StringBuffer sb = new StringBuffer();
        sb.append(DateUtils.getTimeCode());
        sb.append(getRandom(length));
        return sb.toString();
    }

    public static String qiNiuUrl(int length){
        StringBuffer sb = new StringBuffer();
        sb.append(DateUtils.getTimeCode());
        sb.append(getRandom(length));
        return sb.toString();
    }

    public static  Integer getRandom(int min,int max){
       return min + (int)(Math.random() * ((max - min) + 1));
    }

    /**
     * // 入库之前，使用 Base64 编码
     * @param nickName
     * @return
     * @throws UnsupportedEncodingException
     */
    public static  String  base64NikeName(String nickName) throws UnsupportedEncodingException{
        if(StringUtils.isEmpty(nickName)){
            return "";
        }
        // 入库之前，使用 Base64 编码
        return Base64.encodeBase64String(nickName.getBytes("utf-8"));
    }

    /**
     * // 出库后，使用 Base64 解码
     * @param nickName
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String base64DecodeNikeName(String nickName)throws UnsupportedEncodingException{
        return new String(Base64.decodeBase64(nickName.getBytes()), "utf-8");
    }


    /**
     * 获取省市代码字符串
     * @param province 省代码
     * @param city 市代码
     * @return 组合
     */
    public static String getCityCode(String province, String city){
        if(StringUtils.isEmpty(province) || StringUtils.isEmpty(city)){
            return "";
        }
        return province+","+city;
    }

    public static String formatManey(Double money, Integer length) {
        if ( money==null || money == 0  ) {
            return "0";
        }
        NumberFormat formater = null;
        if (length == null || length==0 ) {
            formater = new DecimalFormat("###,###.00");

        } else {
            StringBuffer buff = new StringBuffer();
            buff.append("###,###.");
            for (int i = 0; i < length; i++) {
                buff.append("0");
            }
            formater = new DecimalFormat(buff.toString());
        }
        return formater.format(money);
    }
}
